#include "Planets.h"
#include "Zone.h"
#include "GamePlayState.h"
#include "Projectiles.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "Player.h"
#include "DestroyEntityMessage.h"
#include "ShadowHook.h"
#include "Game.h"

Planets::Planets()
{
	image = SGD::GraphicsManager::GetInstance()->LoadTexture("resources/images/Planetoids.png", {255, 255, 255, 255 });
	sandPlanet = SGD::GraphicsManager::GetInstance()->LoadTexture("resources/images/desert_planetoid.png");
	forestPlanet = SGD::GraphicsManager::GetInstance()->LoadTexture("resources/images/green_planitoid.png");
	icePlanet = SGD::GraphicsManager::GetInstance()->LoadTexture("resources/images/ice_planetoid.png");
	shadowPlanet = SGD::GraphicsManager::GetInstance()->LoadTexture("resources/images/purple_planitoid.png");
	lavaPlanet = SGD::GraphicsManager::GetInstance()->LoadTexture("resources/images/lava-planitoid.png");
}

Planets::~Planets()
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(image);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(sandPlanet);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(forestPlanet);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(icePlanet);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(shadowPlanet);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(lavaPlanet);

}

void Planets::Update(float elapsedTime)
{

}

void Planets::Render()
{
	SGD::Rectangle camOffset{ { Game::GetInstance()->GetCameraPos().x - 100,
		Game::GetInstance()->GetCameraPos().y - 100 },
		SGD::Size{ (Game::GetInstance()->GetScreenWidth() + 500)/Game::GetInstance()->GetZoomLevel(), 
		(Game::GetInstance()->GetScreenHeight() + 500) / Game::GetInstance()->GetZoomLevel() } };
	SGD::Rectangle posRect = { this->GetRect().ComputeCenter(), SGD::Size{ 1, 1 } };
	if (camOffset.IsIntersecting(posRect))
	{
		collisionFlag = 0;
		SGD::GraphicsManager* graphics = SGD::GraphicsManager::GetInstance();
		SGD::Rectangle rect = { SGD::Point{ position.x - Game::GetInstance()->GetCameraPos().x,
			position.y - Game::GetInstance()->GetCameraPos().y }, size };
		//SGD::GraphicsManager::GetInstance()->DrawRectangle(rect, SGD::Color{ 0, 0, 255 });

		SGD::Point renderPos{ (position.x - Game::GetInstance()->GetCameraPos().x) * Game::GetInstance()->GetZoomLevel(),
			(position.y - Game::GetInstance()->GetCameraPos().y) * Game::GetInstance()->GetZoomLevel() };

		SGD::Rectangle renderRect;
		if (planetType == SAND)
		{
			//renderRect = { 18, 153, 143, 278 };
			graphics->DrawTexture(sandPlanet, renderPos, 0.0f, {}, {},
				(SGD::Size{ Game::GetInstance()->GetZoomLevel() * .5f, Game::GetInstance()->GetZoomLevel() * .5f }));
		}
		else if (planetType == FOREST)
		{
			//renderRect = { 18, 8, 143, 133 };
			graphics->DrawTexture(forestPlanet, renderPos, 0.0f, {}, {},
				(SGD::Size{ Game::GetInstance()->GetZoomLevel(), Game::GetInstance()->GetZoomLevel() }));
		}
		else if (planetType == ICE)
		{
			//renderRect = { 416, 8, 541, 133 };
			graphics->DrawTexture(icePlanet, renderPos, 0.0f, {}, {},
				(SGD::Size{ Game::GetInstance()->GetZoomLevel(), Game::GetInstance()->GetZoomLevel() }));
		}
		else if (planetType == LAVA)
		{
			//renderRect = { 18, 288, 143, 414 };
			graphics->DrawTexture(lavaPlanet, renderPos, 0.0f, {}, {},
				(SGD::Size{ Game::GetInstance()->GetZoomLevel(), Game::GetInstance()->GetZoomLevel() }));
		}
		else if (planetType == SHADOW)
		{
			//renderRect = { 18, 433, 143, 558 };
			graphics->DrawTexture(shadowPlanet, renderPos, 0.0f, {}, {},
				(SGD::Size{ Game::GetInstance()->GetZoomLevel() *.5f, Game::GetInstance()->GetZoomLevel() *.5f }));
		}
		else if (planetType == ASTD_SAND)
		{
			//renderRect = { 316, 198, 362, 243 };
			//graphics->DrawTextureSection(image, renderPos, renderRect, 0.0f, {}, {},
			//	(SGD::Size{ Game::GetInstance()->GetZoomLevel(), Game::GetInstance()->GetZoomLevel() }));
			graphics->DrawTexture(sandPlanet, renderPos, 0.0f, {}, {255,125,125,125},
				(SGD::Size{ Game::GetInstance()->GetZoomLevel() * .125f, Game::GetInstance()->GetZoomLevel() * .125f }));
		}
		else if (planetType == ASTD_FOREST)
		{
			//renderRect = { 311, 53, 357, 98 };
			//graphics->DrawTextureSection(image, renderPos, renderRect, 0.0f, {}, {},
			//	(SGD::Size{ Game::GetInstance()->GetZoomLevel(), Game::GetInstance()->GetZoomLevel() }));
			graphics->DrawTexture(forestPlanet, renderPos, 0.0f, {}, { 255, 125, 125, 125 },
				(SGD::Size{ Game::GetInstance()->GetZoomLevel()  * .25f, Game::GetInstance()->GetZoomLevel()  * .25f }));
		}
		else if (planetType == ASTD_ICE)
		{
			//renderRect = { 416, 66, 541, 111 };
			//graphics->DrawTextureSection(image, renderPos, renderRect, 0.0f, {}, {},
			//	(SGD::Size{ Game::GetInstance()->GetZoomLevel(), Game::GetInstance()->GetZoomLevel() }));
			graphics->DrawTexture(icePlanet, renderPos, 0.0f, {}, { 255, 125, 125, 125 },
				(SGD::Size{ Game::GetInstance()->GetZoomLevel()  * .25f, Game::GetInstance()->GetZoomLevel()  * .25f }));
		}
		else if (planetType == ASTD_LAVA)
		{
			//renderRect = { 311, 333, 358, 378 };
			//graphics->DrawTextureSection(image, renderPos, renderRect, 0.0f, {}, {},
			//	(SGD::Size{ Game::GetInstance()->GetZoomLevel(), Game::GetInstance()->GetZoomLevel() }));
			graphics->DrawTexture(lavaPlanet, renderPos, 0.0f, {}, { 255, 125, 125, 125 },
				(SGD::Size{ Game::GetInstance()->GetZoomLevel() * .25f, Game::GetInstance()->GetZoomLevel() * .25f }));
		}
		else if (planetType == ASTD_SHADOW)
		{
			//renderRect = { 311, 478, 358, 523 };
			//graphics->DrawTextureSection(image, renderPos, renderRect, 0.0f, {}, {},
			//	(SGD::Size{ Game::GetInstance()->GetZoomLevel(), Game::GetInstance()->GetZoomLevel() }));
			graphics->DrawTexture(shadowPlanet, renderPos, 0.0f, {}, { 255, 125, 125, 125 },
				(SGD::Size{ Game::GetInstance()->GetZoomLevel() * .125f, Game::GetInstance()->GetZoomLevel() * .125f }));
		}

		
	}
	else
		collisionFlag = -1;
}

void Planets::HandleCollision(const IEntity* other)
{
	/*if (other->GetType() == ENT_GRAPPLE)
	{
		const ShadowHook* p = dynamic_cast<const ShadowHook*>(other);
		if (p->GetAttached())
		{
			Player * player = dynamic_cast<Player*>(p->GetOwner());
			player->SetDestinationPlanet(this);
			player->SetCurrentPlanet(nullptr);
			player->SetGrappleActive(false);
		}
	}*/
}